﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using WebshopPizzaRio.App_Code.BLL;


namespace WebshopPizzaRio
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        public string SearchSQL;

        protected void Page_Load(object sender, EventArgs e)
        {
            MembershipUser currentuserid = Membership.GetUser();

            try
            {
                if ((Guid)currentuserid.ProviderUserKey == null)
                {
                    throw new NullReferenceException();
                }
                else if (!Roles.IsUserInRole("Administrators"))
                {
                    Response.Redirect("Voorpagina.aspx");
                }
            }
            catch (NullReferenceException)
            {
                Response.Redirect("Voorpagina.aspx");
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            FormView1.Visible = true;
            GridView1.Visible = false;
            LinkButton1.Visible = true;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            LinkButton1.Visible = false;
            Label1.Visible = false;

            Boolean formvis = true;

            if (FormView1.Visible == true)
            {
                //formvis = true;
            }
            else if (FormView1.Visible == false)
            {
                //formvis = false;
            }

            switch (formvis)
            {
                case true:
                    FormView1.Visible = false;
                    FormView1.ChangeMode(FormViewMode.Edit);
                    GridView1.Visible = true;
                    break;
                case false:
                    FormView1.Visible = true;
                    GridView1.Visible = false;
                    break;
            }

            string searchvn = "Membership_NaamAdresWoonplaats.[Voornaam] = '" + TbVoornaam.Text + "'";
            string searchan = "Membership_NaamAdresWoonplaats.[Achternaam] = '" + TbAchternaam.Text + "'";
            string searchad = "Membership_NaamAdresWoonplaats.[Adres] = '" + TbAdres.Text + "'";
            string searchwp = "Membership_NaamAdresWoonplaats.[Woonplaats] = '" + TbWoonplaats.Text + "'";
            string searchtf = "Membership_NaamAdresWoonplaats.[Telefoon] = '" + TbTelefoon.Text + "'";

            string searchvnA = "";
            string searchanA = "";
            string searchadA = "";
            string searchwpA = "";
            string searchtfA = "";
            int count = 0;

            if (TbVoornaam.Text != "")
            {
                searchvnA = searchvn;
                count += 1;
            }
            if (TbAchternaam.Text != "")
            {
                searchanA = searchan;
                count += 1;
            }
            if (TbAdres.Text != "")
            {
                searchadA = searchad;
                count += 1;
            }
            if (TbWoonplaats.Text != "")
            {
                searchwpA = searchwp;
                count += 1;
            }
            if (TbTelefoon.Text != "")
            {
                searchtfA = searchtf;
                count += 1;
            }

            switch (count)
            {
                case 0:
                    SearchSQL = "SELECT Membership_NaamAdresWoonplaats.*, aspnet_Membership.LoweredEmail FROM Membership_NaamAdresWoonplaats INNER JOIN aspnet_Membership ON Membership_NaamAdresWoonplaats.UserId = aspnet_Membership.UserId";
                    break;
                case 1:
                    SearchSQL = "SELECT Membership_NaamAdresWoonplaats.*, aspnet_Membership.LoweredEmail FROM Membership_NaamAdresWoonplaats INNER JOIN aspnet_Membership ON Membership_NaamAdresWoonplaats.UserId = aspnet_Membership.UserId WHERE " + searchvnA + " " + searchanA + " " + searchadA + " " + searchwpA + " " + searchtfA;
                    break;
                case 2:
                    SearchSQL = "SELECT Membership_NaamAdresWoonplaats.*, aspnet_Membership.LoweredEmail FROM Membership_NaamAdresWoonplaats INNER JOIN aspnet_Membership ON Membership_NaamAdresWoonplaats.UserId = aspnet_Membership.UserId WHERE " + searchvnA + "OR" + searchanA + " " + searchadA + " " + searchwpA + " " + searchtfA;
                    break;
                case 3:
                    SearchSQL = "SELECT Membership_NaamAdresWoonplaats.*, aspnet_Membership.LoweredEmail FROM Membership_NaamAdresWoonplaats INNER JOIN aspnet_Membership ON Membership_NaamAdresWoonplaats.UserId = aspnet_Membership.UserId WHERE " + searchvnA + "OR" + searchanA + "OR" + searchadA + " " + searchwpA + " " + searchtfA;
                    break;
                case 4:
                    SearchSQL = "SELECT Membership_NaamAdresWoonplaats.*, aspnet_Membership.LoweredEmail FROM Membership_NaamAdresWoonplaats INNER JOIN aspnet_Membership ON Membership_NaamAdresWoonplaats.UserId = aspnet_Membership.UserId WHERE " + searchvnA + "OR" + searchanA + "OR" + searchadA + "OR" + searchwpA + " " + searchtfA;
                    break;
                case 5:
                    SearchSQL = "SELECT Membership_NaamAdresWoonplaats.*, aspnet_Membership.LoweredEmail FROM Membership_NaamAdresWoonplaats INNER JOIN aspnet_Membership ON Membership_NaamAdresWoonplaats.UserId = aspnet_Membership.UserId WHERE " + searchvnA + "OR" + searchanA + "OR" + searchadA + "OR" + searchwpA + "OR" + searchtfA;
                    break;
            }

            DataBind();
        }


        protected void EditButton_Click(object sender, EventArgs e)
        {

        }

        protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            GebruikersBLL GBLL = new GebruikersBLL();

            TextBox VN = (TextBox)FormView1.FindControl("VoornaamTextBox");
            TextBox AC = (TextBox)FormView1.FindControl("AchternaamTextBox");
            TextBox AD = (TextBox)FormView1.FindControl("AdresTextBox");
            TextBox WP = (TextBox)FormView1.FindControl("WoonplaatsTextBox");
            TextBox TF = (TextBox)FormView1.FindControl("TelefoonTextBox");
            TextBox EM = (TextBox)FormView1.FindControl("EmailTextBox");
            TextBox GUID = (TextBox)FormView1.FindControl("UserIdTextBox");

            GBLL.UserDataWijzigen(VN.Text, AC.Text, AD.Text, WP.Text, TF.Text, Guid.Parse(GUID.Text));
            GBLL.UserEmailWijzigen(EM.Text, Guid.Parse(GUID.Text));

            GridView1.Visible = false;
            FormView1.Visible = false;
            LinkButton1.Visible = false;
            Label1.Visible = true;
            Label1.Text = "Gegevens zijn aangepast.";
        }
    }
}